package com.bancomer.batch.validators.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import com.bancomer.batch.Constants;
import com.bancomer.batch.pojo.Alert;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.ValidationResult;
import com.bancomer.batch.validators.GeneralValidator;

public class FileLayoutValidator implements GeneralValidator{

	@Override
	public ValidationResult executeValidation(FileHelper fileHelper,
			Object... obj) {
		Alert alert = (Alert)obj[0];
		FileHelper layoutFile = alert.getPropertiesFiles().get(0);
		ValidationResult result = new ValidationResult();
		FileReader input = null;
		BufferedReader buffer = null;
		String line;
		String load="";
		for(FileHelper file:alert.getPropertiesFiles()){
			if(file.getType().equals(Constants.FILE_PIG_LOAD)){
				layoutFile=file;
				break;
			}
		}
		if(layoutFile==null){
			result.setMessage("No existe archivo de Layout. No se puede ejecutar la Alerta "+alert.getName());
			result.setSucces(false);
			return result;
		}
		try{
			input = new FileReader(layoutFile.getLocalPath()+layoutFile.getName());
			buffer = new BufferedReader(input);
			line=buffer.readLine();
			while(line!=null){
				if(line.contains(fileHelper.getProperties().getProperty("layoutName"))){
					load = line;
					break;
				}
				line = buffer.readLine();
			}
			buffer.close();
			if(load.equals("")){
				result.setMessage("No existe Layout para el archivo "+fileHelper.getName());
				result.setSucces(false);
				return result;
			}
		}
		catch(Exception e){
			result.setMessage("Class: FileLayoutValidator - "+e.getMessage());
			result.setSucces(false);
		}
		// TODO Auto-generated method stub
		return result;
	}
	
}
